package com.washmadam.service.service.customer.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.washmadam.service.common.enums.DeleteEnum;
import com.washmadam.service.common.enums.StatusEnum;
import com.washmadam.service.db.dao.CustomerMapper;
import com.washmadam.service.db.dto.Customer;
import com.washmadam.service.db.example.CustomerExample;
import com.washmadam.service.db.example.CustomerExample.Criteria;
import com.washmadam.service.db.request.query.CustomerQueryParam;
import com.washmadam.service.service.customer.CustomerQueryService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CustomerQueryServiceImpl implements CustomerQueryService {
	@Autowired
	private CustomerMapper customerMapper;

	@Override
	public PageInfo<Customer> getCustomerList(CustomerQueryParam requestParam) {
		PageHelper.startPage(requestParam.getPageNum(), requestParam.getPageSize());
		CustomerExample example = new CustomerExample();
		Criteria criteria = example.createCriteria();
		//客户姓名
		if(StringUtils.isNotBlank(requestParam.getCustomerName())) {
			criteria.andCustomerNameLike("%"+requestParam.getCustomerName()+"%");
		}
		//客户手机号
		if(StringUtils.isNotBlank(requestParam.getContactMobile())) {
			criteria.andContactMobileLike("%"+requestParam.getContactMobile()+"%");
		}
		criteria.andStatusEqualTo(StatusEnum.ENABLE.getCode())
			   .andIsDeleteEqualTo(DeleteEnum.NORMAL.getCode());
		example.setOrderByClause("id desc");
		List<Customer> list = customerMapper.selectByExample(example);
		PageInfo<Customer> page = new PageInfo<>(list);
		return page;
	}
}
